/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function validarFormulario(formulario, rutaProfesor){
    
        $.validator.addMethod("username",function(value,element){
            return this.optional(element) || /^[a-zA-Z0-9._-]{3,16}$/i.test(value);
        },"Username de 3-15 caracteres");
        
        $.validator.addMethod("dniValido",function(value){
            var retorno = "false";
            $.ajax({
                type:"POST",
                url: rutaProfesor,
                data: {'dni':value, 'option':'dni'},
                dataType: 'json',
                async: false,
                success:function(result){
                    if(result.exist){
                        retorno = "false";
                    } else{
                        retorno = "true";
                    }
                }
            });
            
            if(retorno === "true"){
                return true;
            } else {
                return false;
            }
        },"Sorry, this user name is not available");
        
        $.validator.addMethod("existUsername",function(value){
            var retorno = "false";
            $.ajax({
                type:"POST",
                url: rutaProfesor,
                data: {'userName':value, 'option':'username'},
                dataType: 'json',
                async: false,
                success:function(result){
                    if(result.exist){
                        retorno = "false";
                    } else{
                        retorno = "true";
                    }
                }
            });
            
            if(retorno === "true"){
                return true;
            } else {
                return false;
            }
        },"Sorry, this user name is not available");
        
        $.validator.addMethod("password",function(value,element){
            return this.optional(element) || /^[A-Za-z0-9!@#$%^&*()_]{6,16}$/i.test(value);
        },"Passwords de 6-16 caracteres");
        
        $.validator.addMethod("onlyString", function (value,element){
            return this.optional(element) || /^[a-záéíóúñ A-ZáéíóúÑ]{3,16}$/i.test(value);
        },"Cadena de 3-16 caracteres, sin números ni símbolos");
        
        $.validator.addMethod("formatDate", function (value,element){
            return this.optional(element) || /^[0-9/]{10,10}$/i.test(value);
        },"<div class='text-error'>El formato de la fecha debe ser dd/mm/aaaa <i class='text-error icon-hand-up'></i></div>");
        
        formulario.validate({
            rules:{
                "pyaf_gymbundle_profesortype[persona][nombre]":{
                    required: true,
                    onlyString: true
                },
                "pyaf_gymbundle_profesortype[persona][apellido]":{
                    required: true,
                    onlyString: true
                },
                "pyaf_gymbundle_profesortype[persona][dni]":{
                    required: true,
                    number: true,
                    rangelength: [7, 8],
                    dniValido: true
                },
                "pyaf_gymbundle_profesortype[persona][fecha_nacimiento]":{
                    required: true,
                    date:true,
                    formatDate:true
                },
                "pyaf_gymbundle_profesortype[persona][telefono]":{
                    required: true,
                    number:true,
                    rangelength: [7, 7]
                },
                "pyaf_gymbundle_profesortype[persona][celular]":{
                    required: true,
                    number:true,
                    rangelength: [10, 10]
                },
                "pyaf_gymbundle_profesortype[persona][direccion]":{
                    required: true
                },
                "pyaf_gymbundle_profesortype[persona][email]":{
                    required: true,
                    email: true
                },
                "pyaf_gymbundle_profesortype[username]":{
                    required: true,
                    username: true,
                    existUsername: true
                },
                "pyaf_gymbundle_profesortype[userRoles][]":{
                    required: true
                },
                "pyaf_gymbundle_profesortype[persona][anexo]":{
                    required: true
                },
                "pyaf_gymbundle_profesortype[password][first]":{
                    required: true,
                    password: true
                },
                "pyaf_gymbundle_profesortype[password][second]":{
                    required: true,
                    equalTo: "#pyaf_gymbundle_profesortype_password_first"
                }
            },
            messages:{
                "pyaf_gymbundle_profesortype[persona][nombre]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    onlyString: "<div class='text-error'>Longitud requerida 3-16, o contine caracteres no permitidos! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][apellido]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    onlyString: "<div class='text-error'>Longitud requerida 3-16, o contine caracteres no permitidos! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][dni]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                    rangelength: "<div class='text-error'>Longitud requerida 7-8! <i class='text-error icon-hand-up'></i></div>",
                    dniValido: "<div class='text-error'>Error, disculpe, pero ya existe una persona con este DNI! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][fecha_nacimiento]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    date: "<div class='text-error'>Por favor ingrese una fecha correcta! (dd/mm/aaaa) <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][telefono]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                    rangelength: "<div class='text-error'>Longitud requerida 7! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][celular]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                    rangelength: "<div class='text-error'>Longitud requerida 10! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][direccion]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][anexo]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][email]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    email: "<div class='text-error'>Formato de E-mail incorrecto! <i class='text-error icon-hand-up'></i></div>",
                },
                "pyaf_gymbundle_profesortype[username]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    username: "<div class='text-error'>Longitud requerida 3-16, o contiene caracteres no permitidos! <i class='text-error icon-hand-up'></i></div>",
                    existUsername: "<div class='text-error'>Existe un usuario con este nombre, intente de nuevo! <i class='text-error icon-hand-up'></i></div>"
                    
                },
                "pyaf_gymbundle_profesortype[userRoles][]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                },
                "pyaf_gymbundle_profesortype[password][first]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    password: "<div class='text-error'>Longitud requerida 6-16! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[password][second]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    equalTo: "<div class='text-error'>No coincide con la contraseña anterior! <i class='text-error icon-hand-up'></i></div>",
                    password: "<div class='text-error'>Longitud requerida 6-16! <i class='text-error icon-hand-up'></i></div>"
                }
            },
            onkeyup:false,
            submitHandler: function(form){
                form.submit();
            },
            invalidHandler: function(){
        
            }
        });
}
function validarFormularioEditar(formulario, rutaProfesor){
    
        $.validator.addMethod("username",function(value,element){
            return this.optional(element) || /^[a-zA-Z0-9._-]{3,16}$/i.test(value);
        },"Username de 3-15 caracteres");
 
        $.validator.addMethod("password",function(value,element){
            return this.optional(element) || /^[A-Za-z0-9!@#$%^&*()_]{6,16}$/i.test(value);
        },"Passwords de 6-16 caracteres");
        
        $.validator.addMethod("onlyString", function (value,element){
            return this.optional(element) || /^[a-záéíóúñ A-ZáéíóúÑ]{3,16}$/i.test(value);
        },"Cadena de 3-16 caracteres, sin números ni símbolos");
        
        $.validator.addMethod("formatDate", function (value,element){
            return this.optional(element) || /^[0-9/]{10,10}$/i.test(value);
        },"<div class='text-error'>El formato de la fecha debe ser dd/mm/aaaa <i class='text-error icon-hand-up'></i></div>");
        
        formulario.validate({
            rules:{
                "pyaf_gymbundle_profesortype[persona][nombre]":{
                    required: true,
                    onlyString: true
                },
                "pyaf_gymbundle_profesortype[persona][apellido]":{
                    required: true,
                    onlyString: true
                },
                "pyaf_gymbundle_profesortype[persona][fecha_nacimiento]":{
                    required: true,
                    date:true,
                    formatDate:true
                },
                "pyaf_gymbundle_profesortype[persona][telefono]":{
                    required: true,
                    number:true,
                    rangelength: [7, 7]
                },
                "pyaf_gymbundle_profesortype[persona][celular]":{
                    required: true,
                    number:true,
                    rangelength: [10, 10]
                },
                "pyaf_gymbundle_profesortype[persona][direccion]":{
                    required: true
                },
                "pyaf_gymbundle_profesortype[persona][email]":{
                    required: true,
                    email: true
                },
                "pyaf_gymbundle_profesortype[userRoles][]":{
                    required: true
                },
                "pyaf_gymbundle_profesortype[persona][anexo]":{
                    required: true
                },
                "pyaf_gymbundle_profesortype[password][first]":{
                    required: true,
                    password: true
                },
                "pyaf_gymbundle_profesortype[password][second]":{
                    required: true,
                    equalTo: "#pyaf_gymbundle_profesortype_password_first"
                }
            },
            messages:{
                "pyaf_gymbundle_profesortype[persona][nombre]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    onlyString: "<div class='text-error'>Longitud requerida 3-16, o contine caracteres no permitidos! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][apellido]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    onlyString: "<div class='text-error'>Longitud requerida 3-16, o contine caracteres no permitidos! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][fecha_nacimiento]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    date: "<div class='text-error'>Por favor ingrese una fecha correcta! (dd/mm/aaaa) <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][telefono]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                    rangelength: "<div class='text-error'>Longitud requerida 7! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][celular]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                    rangelength: "<div class='text-error'>Longitud requerida 10! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][direccion]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][anexo]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[persona][email]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    email: "<div class='text-error'>Formato de E-mail incorrecto! <i class='text-error icon-hand-up'></i></div>",
                },
                "pyaf_gymbundle_profesortype[userRoles][]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                },
                "pyaf_gymbundle_profesortype[password][first]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    password: "<div class='text-error'>Longitud requerida 6-16! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_profesortype[password][second]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    equalTo: "<div class='text-error'>No coincide con la contraseña anterior! <i class='text-error icon-hand-up'></i></div>",
                    password: "<div class='text-error'>Longitud requerida 6-16! <i class='text-error icon-hand-up'></i></div>"
                }
            },
            onkeyup:false,
            submitHandler: function(form){
                form.submit();
            },
            invalidHandler: function(){
        
            }
        });
}
function validarFormularioAlumno(formulario, rutaAlumno, editar){
    
        $.validator.addMethod("username",function(value,element){
            return this.optional(element) || /^[a-zA-Z0-9._-]{3,16}$/i.test(value);
        },"Username de 3-15 caracteres");
        
        $.validator.addMethod("dniValido",function(value){
            var retorno = false;
            $.ajax({
                type:"POST",
                url: rutaAlumno,
                data: {'dni':value},
                dataType: 'json',
                async: false,
                success:function(result){
                    if(result.exist){
                        retorno = false;
                    } else{
                        retorno = true;
                    }
                }
            });
            
            if(editar === true){retorno = !retorno;} 
            return retorno;
            
        },"Sorry, this user name is not available");
        
        $.validator.addMethod("password",function(value,element){
            return this.optional(element) || /^[A-Za-z0-9!@#$%^&*()_]{6,16}$/i.test(value);
        },"Passwords de 6-16 caracteres");
        
        $.validator.addMethod("onlyString", function (value,element){
            return this.optional(element) || /^[a-záéíóúñ A-ZáéíóúÑ]{3,16}$/i.test(value);
        },"Cadena de 3-16 caracteres, sin números ni símbolos");
        
        $.validator.addMethod("formatDate", function (value,element){
            return this.optional(element) || /^[0-9/]{10,10}$/i.test(value);
        },"<div class='text-error'>El formato de la fecha debe ser dd/mm/aaaa <i class='text-error icon-hand-up'></i></div>");
        
        formulario.validate({
            rules:{
                "pyaf_gymbundle_alumnotype[persona][nombre]":{
                    required: true,
                    onlyString: true
                },
                "pyaf_gymbundle_alumnotype[persona][apellido]":{
                    required: true,
                    onlyString: true
                },
                "pyaf_gymbundle_alumnotype[persona][dni]":{
                    required: true,
                    number: true,
                    rangelength: [7, 8],
                    dniValido: true
                },
                "pyaf_gymbundle_alumnotype[persona][telefono]":{
                    required: true,
                    number:true,
                    rangelength: [7, 7]
                },
                "pyaf_gymbundle_alumnotype[persona][celular]":{
                    required: true,
                    number:true,
                    rangelength: [10, 10]
                },
                "pyaf_gymbundle_alumnotype[persona][direccion]":{
                    required: true
                },
                "pyaf_gymbundle_alumnotype[persona][email]":{
                    required: true,
                    email: true
                },
                "pyaf_gymbundle_alumnotype[promocion]":{
                    required: true
                },
                "pyaf_gymbundle_alumnotype[cuota]":{
                    required: true,
                    number:true
                },
                "pyaf_gymbundle_alumnotype[persona][anexo]":{
                    required: true
                }
            },
            messages:{
                "pyaf_gymbundle_alumnotype[persona][nombre]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    onlyString: "<div class='text-error'>Longitud requerida 3-16, o contine caracteres no permitidos! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_alumnotype[persona][apellido]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    onlyString: "<div class='text-error'>Longitud requerida 3-16, o contine caracteres no permitidos! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_alumnotype[persona][dni]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                    rangelength: "<div class='text-error'>Longitud requerida 7-8! <i class='text-error icon-hand-up'></i></div>",
                    dniValido: "<div class='text-error'>Error, disculpe, pero ya existe una persona con este DNI! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_alumnotype[persona][telefono]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                    rangelength: "<div class='text-error'>Longitud requerida 7! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_alumnotype[persona][celular]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                    rangelength: "<div class='text-error'>Longitud requerida 10! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_alumnotype[persona][direccion]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_alumnotype[persona][anexo]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_alumnotype[persona][email]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    email: "<div class='text-error'>Formato de E-mail incorrecto! <i class='text-error icon-hand-up'></i></div>",
                },
                "pyaf_gymbundle_alumnotype[promocion]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>"
                },
                "pyaf_gymbundle_alumnotype[cuota]":{
                    required: "<div class='text-error'>Campo requerido! <i class='text-error icon-hand-up'></i></div>",
                    number: "<div class='text-error'>Campo de formato numérico! <i class='text-error icon-hand-up'></i></div>",
                }
            },
            onkeyup:false,
            submitHandler: function(form){
                form.submit();
            },
            invalidHandler: function(){
        
            }
        });
}
